package com.cinsea.chart.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.common.Page;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChartAction extends ExtAction {
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        if (action.equals("getchart")) {
            String type=StringUtil.null2String(request.getParameter("type"));
            if (type.equals("line")||type.equals("column")||type.equals("bar")||type.equals("pie")) {
                String title=StringUtil.null2String(request.getParameter("title"));
                String ytitle=StringUtil.null2String(request.getParameter("ytitle"));
                String sql=StringUtil.null2String(request.getParameter("sql"));
                Map result=new HashMap();
                //config
                Map chartmap=new HashMap();
                chartmap.put("type",type);
                result.put("chart",chartmap);
                Map creditsmap=new HashMap();
                creditsmap.put("enabled",false);
                result.put("credits",creditsmap);
                Map exportingmap=new HashMap();
                exportingmap.put("enabled",false);
                result.put("exporting",exportingmap);
                Map titlemap=new HashMap();
                titlemap.put("text",title);
                titlemap.put("x",-20);
                result.put("title",titlemap);
                List categories = new ArrayList();
                if (!type.equals("pie")) {
                    Map tooltipmap=new HashMap();
                    tooltipmap.put("enabled",false);
                    result.put("tooltip",tooltipmap);
                    Map yaxismap=new HashMap();
                    Map yaxistitlemap=new HashMap();
                    yaxistitlemap.put("text",ytitle);
                    yaxismap.put("title",yaxistitlemap);
                    Map plotlinesmap=new HashMap();
                    plotlinesmap.put("value",0);
                    plotlinesmap.put("width",1);
                    plotlinesmap.put("color","#808080");
                    List plotlineslist=new ArrayList();
                    plotlineslist.add(plotlinesmap);
                    yaxismap.put("plotLines",plotlineslist);
                    result.put("yAxis",yaxismap);

                    Map xaxismap = new HashMap();

                    xaxismap.put("categories", categories);
                    result.put("xAxis", xaxismap);
                }

                Map legendmap=new HashMap();
                legendmap.put("enabled",false);
            /*legendmap.put("layout","vertical");
            legendmap.put("align","right");
            legendmap.put("verticalAlign","top");
            legendmap.put("x",-10);
            legendmap.put("y",100);
            legendmap.put("borderWidth",0);*/
                result.put("legend",legendmap);



                List series=new ArrayList();
                result.put("series",series);
                Map seriemap=new HashMap();
                List serie=new ArrayList();
                seriemap.put("name","value");
                seriemap.put("data",serie);
                series.add(seriemap);
                List<Map> datas=jdbcDao.queryForList(sql);
                for(Map data:datas){
                    String name=String.valueOf(data.get("name"));
                    int value=NumberUtil.getIntegerValue(data.get("value"), -1);
                    if (!type.equals("pie")) {
                        categories.add(name);
                        serie.add(value);
                    }else{
                        List block=new ArrayList();
                        block.add(name);
                        block.add(value);
                        serie.add(block);
                    }
                }
                response.getWriter().print(JSON.toJSONString(result));
                return;
            }
        }
    }
}
